package com.ztesoft.huangfu.learning.springboot.springbootmail.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created with IntelliJ IDEA.
 * Author: huangfu.daohui
 * Date: 2018/5/27
 * Time: 21:50
 * Version: 1.0
 * Description: 发送邮件服务
 * 参考：http://www.cnblogs.com/ityouknow/p/6823356.html
 **/
@RestController
@RequestMapping("/mail")
@Slf4j
public class MailSenderController {

    @Autowired
    private JavaMailSender mailSender;

    @GetMapping("/sendemail")
    public void sendSimpleMail() {
        SimpleMailMessage message = new SimpleMailMessage();
        // 参数设置在配置文件yml中，用@value获取
        message.setFrom("huangfudaohui@126.com");
        message.setTo("1005067088@qq.com");
        message.setSubject("测试邮件主题");
        message.setText("测试邮件内容");

        try {
            mailSender.send(message);
            log.info("简单邮件已经发送。");
        } catch (Exception e) {
            log.error("发送简单邮件时发生异常！", e);
        }

    }
}
